package doInterce.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RuntimeUtil;
import doInterce.DoInterface;
import utils.DBUtil;
import utils.YmlUtil;

import java.io.File;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 查找辞去工资的照片
 * @Description:
 * @Author: syh
 * @Date: 2024/01/10/16:47
 */
public class DoFindC01Photo implements DoInterface {

    /**
     * 1、使用7z进行解压，把所有的照片都解压到当前目录
     * 2、查找所有的辞去工资记录，并挨个去查找照片是否存在
     * 2.1、如果不存在的话，从解压目录中找到它并且拷贝
     * 2.2、如果存在就跳过
     */
    @Override
    public void doExecute() {
        Map<String, Object> ymlMap = YmlUtil.getYmlMap();
        String czZipDir = Convert.toStr(ymlMap.get("czZipDir")); //辞去工资的解压目录
        String sevenzPath = Convert.toStr(ymlMap.get("sevenzPath")); //7z的目录
        String czPhoto = Convert.toStr(ymlMap.get("czPhoto")); //辞去工资的照片目录
        String czLog = Convert.toStr(ymlMap.get("czLog")); //辞去公职的相关日志输出路径

        String url = Convert.toStr(ymlMap.get("url"));
        String userName = Convert.toStr(ymlMap.get("userName"));
        String password = Convert.toStr(ymlMap.get("password"));
        File czLogFile = new File(czLog);

        List<String> contentList = new ArrayList<>();
        List<String> copyFileNameList = new ArrayList<>();

        try(Connection conn = DBUtil.getConnection(url,userName,password)) {
            File czZipDirFile = new File(czZipDir);
            File sevenzPathFile = new File(sevenzPath);
            File czPhotoFile = new File(czPhoto);

            if(!czZipDirFile.exists()) {
                contentList.add("辞去公职目录为空！");
                throw new Exception("辞去公职目录为空!");
            }
            if(!sevenzPathFile.exists()) {
                contentList.add("7z目录为空！");
                throw new Exception("7z目录为空!");
            }
            if(!czPhotoFile.exists()) {
                contentList.add("辞去公职图片目录为空！");
                throw new Exception("辞去公职图片目录为空!");
            }


            //把文件拓展名改成zip
//            File[] files = czZipDirFile.listFiles();
//            for(File eleFile : files){
//                String name = eleFile.getName();
//                if(name.endsWith("hzbcz")){
//                    contentList.add("查找"+name+"到拓展为hzbcz！");
//                    String newFilePath = eleFile.getAbsolutePath().replace("hzbcz", "zip");
//                    FileUtil.copy(eleFile,new File(newFilePath),true);
//                    contentList.add("拷贝"+name+"成功！");
//                    FileUtil.del(eleFile);
//                    contentList.add("删除"+name+"成功！");
//                }
//            }


            String command = sevenzPath+"\\7z x %s -pZ82z3H4b5z6d24b7 -o%s";
            //进行解压
            File[] files = czZipDirFile.listFiles();
            for(File eleFile : files){
                String name = eleFile.getName();
                System.out.println(String.format(command,eleFile.getAbsolutePath(),czZipDirFile.getAbsolutePath()));
                RuntimeUtil.exec(String.format(command,eleFile.getAbsolutePath(),czZipDirFile.getAbsolutePath()));
                contentList.add("解压"+name+"成功！");
            }


            //查找所有的C01
            Statement statement = conn.createStatement();
            ResultSet resultSet = statement.executeQuery("select A0000,A0101,A5714 from C01 ");
            List<Map<String, String>> c01MapList = DBUtil.getFieldMapList(resultSet, resultSet.getMetaData());
            contentList.add("查找到C01总"+c01MapList.size()+"条！");

            contentList.add("开始查找文件并复制！");
            for(Map<String,String> eleMap : c01MapList){
                File photoFile = new File(czPhoto + File.separator + eleMap.get("A5714"));
                if(!photoFile.exists()){
                    int subIndex = eleMap.get("A5714").lastIndexOf("\\");
                    if(subIndex > 0){
                        eleMap.put("A5714",eleMap.get("A5714").substring(subIndex+1));
                    }
                    File czZipPhotoFile = new File(czZipDir + File.separator + "Photos" + File.separator+ eleMap.get("A5714"));
                    if(czZipPhotoFile.exists()){
                        FileUtil.copy(czZipPhotoFile,photoFile,true);
                        contentList.add("拷贝文件成功："+eleMap.get("A0101"));
                        copyFileNameList.add(photoFile.getAbsolutePath());
                    }else{
                        contentList.add("文件不存在："+eleMap.get("A0101"));
                        contentList.add(czZipPhotoFile.getAbsolutePath());
                    }
                }
                copyFileNameList.add(photoFile.getAbsolutePath());
            }

        File logFile = new File(czLogFile.getAbsolutePath() + File.separator + "log.txt");
        File copyFile = new File(czLogFile.getAbsolutePath() + File.separator + "copyFile.txt");
        if(!logFile.exists()){
            logFile.createNewFile();
        }
        if(!copyFile.exists()){
            copyFile.createNewFile();
        }
        FileUtil.writeLines(contentList, czLogFile.getAbsolutePath() + File.separator + "log.txt","utf-8");
        FileUtil.writeLines(copyFileNameList, czLogFile.getAbsolutePath() + File.separator + "copyFile.txt","utf-8");

        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
